home *** CD-ROM | disk | FTP | other *** search
- Path: morgan.cnu.edu!dlabell
- From: dlabell@pcs.cnu.edu (Daniel LaBell)
- Newsgroups: comp.lang.c++
- Subject: casting a void pointer back to a function pointer
- Date: 04 Apr 1996 07:10:45 GMT
- Organization: CNU
- Message-ID: <DLABELL.96Apr4021045@columbia.pcs.cnu.edu>
- NNTP-Posting-Host: columbia.pcs.cnu.edu
-
- I'm going to skip explaining why I want to do this, and get right to the
- problem. How do I cast a pointer to a function pointer?
-
- Here is trivial example that shows the problem.
-
- void foo1(){ cout << " foo1 " << endl; }
- void foo2(){ cout << " foo2 " << endl; }
- void foo3( int x )
- { cout << " foo3, argument = " << x << endl; }
-
- void do_a_foo( void (*fun)() )
- { (*fun)(); }
- void do_a_foo2( int x, void (*fun) (int ) )
- { (*fun) ( x ); }
- int main()
- {
- do_a_foo ( foo1 );
- do_a_foo ( foo2 );
- do_a_foo2 ( 2, foo3 );
- void * x=foo2;
- do_a_foo (x); // I don't know the syntax to do this cast. :(
- return 0;
- }
- I get this warning message from g++:
- :/home/student/dlabell/C/test.cc: In function `int main()':
- :/home/student/dlabell/C/test.cc:21: warning: ANSI C++ forbids implicit conversion from `void *' in argument passing
- :
- :Compilation finished at Thu Apr 4 01:18:20
-
- So, now my program isn't portable, and if I overload do_a_foo instead of
- making do_a_foo2, it wouldn't compile at all. So, I need to know the syntax
- for casting a pointer to a pointer to a function. Can this be done in a
- portable way? ( how about for ANSI C? ) If not, can this be done in
- g++/gcc? And also if it isnt ANSI wouldn't that violate the premise that
- "any pointer can be cast to void * and back again without loss of
- information"? Or can I declare a type of pointer to a function?
-
-
- --
- Daniel LaBell
-